package com.sskj.b70.v.fragment;

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.b70.R;
import com.sskj.b70.adapter.slimadapter.IViewInjector;
import com.sskj.b70.adapter.slimadapter.SlimAdapter;
import com.sskj.b70.adapter.slimadapter.SlimInjector;
import com.sskj.b70.common.App;
import com.sskj.b70.common.decoration.DividerLineItemDecoration;
import com.sskj.b70.m.bean.TradePositionListBean;
import com.sskj.b70.p.fragment.TradeDealFragmentPst;
import com.sskj.b70.utils.ClickUtil;
import com.sskj.b70.utils.NumberUtil;
import com.sskj.b70.v.activity.TradeActivity;
import com.sskj.b70.v.base.BaseViewPagerFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;


/**
 * 杠杆交易--成交订单
 * Created by Lee on 2018/2/28 0028.
 */

public class TradeDealFragment extends BaseViewPagerFragment<TradeActivity, TradeDealFragmentPst> {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;
    Unbinder unbinder;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private SlimAdapter slimAdapter;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public TradeDealFragmentPst getPresenter() {
        return new TradeDealFragmentPst();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (slimAdapter == null) {
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity()).setFirstDraw(false));
            slimAdapter = SlimAdapter.create().register(R.layout.recy_item_trade_deal, new SlimInjector<TradePositionListBean>() {
                @Override
                public void onInject(TradePositionListBean data, IViewInjector injector) {
                    SpannableString spannableString17 = new SpannableString(data.getStockName());
                    RelativeSizeSpan relativeSizeSpan = new RelativeSizeSpan(0.7F);
                    ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(
                            Color.parseColor("#AABBCC"));
                    StyleSpan styleSpan = new StyleSpan(Typeface.NORMAL);
                    spannableString17.setSpan(relativeSizeSpan, data.getPriceUnit().length() - 1, data.getStockName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    spannableString17.setSpan(foregroundColorSpan, data.getPriceUnit().length() - 1, data.getStockName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    spannableString17.setSpan(styleSpan, data.getPriceUnit().length() - 1, data.getStockName().length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                    injector.text(R.id.tv_lx, data.getBuyBillType() == 2 ? App.INSTANCE.getString(R.string.strTradeDealFragment1) : App.INSTANCE.getString(R.string.strTradeDealFragment2))
                            .textColor(R.id.tv_lx, getResources().getColor(data.getBuyBillType() == 2 ? R.color.market_down : R.color.market_up))
                            .text(R.id.tv_bz, spannableString17)
                            .text(R.id.tv_time, data.getCreateTime())
                            .text(R.id.tv_order_num, App.INSTANCE.getString(R.string.strTradeDealFragment3) + data.getEntrustNo())
                            .text(R.id.tv_first_left_title, App.INSTANCE.getString(R.string.strTradeDealFragment4))
                            .text(R.id.tv_first_left, NumberUtil.keepNoZoreMax4(data.getBuyPrice()))
                            .text(R.id.tv_first_right, NumberUtil.keepNoZoreMax4(data.getBuyMarginFee()) + "")
                            .text(R.id.tv_second_left, NumberUtil.keepNoZoreMax4(data.getSellPrice()))
                            .text(R.id.tv_second_right, NumberUtil.keepNoZoreMax4(data.getBuyDealFee()) + "")
                            .text(R.id.tv_third_left, data.getEntrustLot())
                            .text(R.id.tv_third_right, data.getTradeType() == 1 ? "--" : NumberUtil.keepNoZoreMax4(data.getWinLoseFee()) + "")
                            .text(R.id.tv_four_left, NumberUtil.keepNoZoreMax4(data.getStopProfit()) + "")
                            .text(R.id.tv_four_right, NumberUtil.keepNoZoreMax4(data.getStopLoss()) + "");
                }
            }).attachTo(recyclerView).updateData(new ArrayList<>());
            ClickUtil.click(llNoData, () -> {
                llNoData.setVisibility(View.GONE);
                refreshLayout.setVisibility(View.VISIBLE);
                refreshLayout.autoRefresh();
            });
            refreshLayout.setOnRefreshListener(refreshLayout -> {
                refreshLayout.finishRefresh();
                mPresenter.loadData();
            });
            refreshLayout.setRefreshHeader(new MaterialHeader(getActivity()));
            refreshLayout.autoRefresh();
        }
    }


    public void updateData(List<TradePositionListBean> data) {
        if (data == null || data.size() == 0) {
            llNoData.setVisibility(View.VISIBLE);
            refreshLayout.setVisibility(View.GONE);
            if (data == null) {
                ((TextView) llNoData.findViewById(R.id.tv_content)).setText(App.INSTANCE.getString(R.string.strTradeDealFragment11));
            } else {
                ((TextView) llNoData.findViewById(R.id.tv_content)).setText(App.INSTANCE.getString(R.string.strTradeDealFragment12));
            }
        } else {
            llNoData.setVisibility(View.GONE);
            refreshLayout.setVisibility(View.VISIBLE);
            slimAdapter.updateData(data);
        }
    }


}
